#include "adbprocess.h"
#include <QProcess>

QString AdbProcess::adb = "adb";

bool AdbProcess::IsDeviceMode(const QString &deviceId)
{
    QString commandLine = "adb get-state";
    QString content = RunCommand(commandLine, deviceId);
    return content == "device";
}

QStringList AdbProcess::GetDeviceList()
{
    QStringList devices;
    QString content = RunCommand("devices", "");
    if (!content.isEmpty())
    {
        content.replace("List of devices attached\n", "");
        const QStringList splits = content.split("\n");
        for (const QString &item : splits)
        {
            QStringList devs = item.split(" ");
            if (devs.length() == 2 && devs[1].trimmed() == "device")
            {
                devices.append(devs[0].trimmed());
            }
        }
    }
    return devices;
}

QString AdbProcess::GetBrand(const QString &deviceId)
{
    return RunCommand("shell getprop ro.product.brand", deviceId);
}

QString AdbProcess::GetModel(const QString &deviceId)
{
    return RunCommand("shell getprop ro.product.model", deviceId);
}

QString AdbProcess::GetName(const QString &deviceId)
{
    return RunCommand("shell getprop ro.config.marketing_name", deviceId);
}

QSize AdbProcess::GetSize(const QString &deviceId)
{
    QString commandLine = "adb shell wm size | grep Override";
    QString content = RunCommand(commandLine, deviceId);
    content = content.replace("Override size:", "").trimmed();
    QStringList sizes = content.split("x");
    int w = sizes.value(0).toInt();
    int h = sizes.value(1).toInt();
    return QSize(w, h);
}

void AdbProcess::Pull(const QString &deviceId, const QString &file, const QString &path)
{
    QString commandLine = QString("adb pull %1 %2").arg(file, path);
    RunCommand(commandLine, deviceId);
}

void AdbProcess::Rm(const QString &deviceId, const QString &file)
{
    QString commandLine = QString("adb shell rm %1").arg(file);
    RunCommand(commandLine, deviceId);
}

void AdbProcess::Screencap(const QString &deviceId, const QString &file)
{
    QString commandLine = QString("adb shell screencap -p %1").arg(file);
    RunCommand(commandLine, deviceId);
}

void AdbProcess::UiautomatorDump(const QString &deviceId, const QString &file)
{
    QString commandLine = QString("adb shell uiautomator dump %1").arg(file);
    RunCommand(commandLine, deviceId);
}

void AdbProcess::EnableShowTouches(const QString &deviceId, bool enable)
{
    QString commandLine = QString("adb shell settings put system show_touches %1").arg(enable ? "1" : "0");
    RunCommand(commandLine, deviceId);
}

void AdbProcess::EnablePointerLocation(const QString &deviceId, bool enable)
{
    QString commandLine = QString("adb shell settings put system pointer_location %1").arg(enable ? "1" : "0");
    RunCommand(commandLine, deviceId);
}

QString AdbProcess::RunCommand(const QString &commandLine, const QString &deviceId)
{
    QStringList cmds = commandLine.split(" ");
    cmds.removeAll("");
    if (commandLine.startsWith("adb "))
    {
        cmds.removeAt(0);
    }
    QStringList commands;
    if (!deviceId.isEmpty())
    {
        commands.append("-s");
        commands.append(deviceId);
    }
    commands.append(cmds);

    QProcess process;
    process.start(adb, commands);
    process.waitForFinished(240000);
    QByteArray qbt = process.readAllStandardOutput();
    QByteArray qbt2 = process.readAllStandardError();
    QString out = QString::fromUtf8(qbt).trimmed();
//    QString err = QString::fromUtf8(qbt2).trimmed();

    ErrorFilter(out);
    return out;
}

void AdbProcess::ErrorFilter(QString &content)
{
    content.replace("\r", "");
    content.replace("\t", " ");

    if (content.contains("* daemon"))
    {
        int pos = content.indexOf("* daemon started successfully *");
        if (pos != -1)
        {
            content = content.mid(pos + 31);
        }
    }
    content = content.trimmed();
}
